package com.lefu.es.util;

import android.content.Context;
import android.support.annotation.NonNull;
import android.util.Log;

import com.google.android.gms.auth.api.signin.GoogleSignIn;
import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
import com.google.android.gms.fitness.Fitness;
import com.google.android.gms.fitness.data.DataPoint;
import com.google.android.gms.fitness.data.DataSet;
import com.google.android.gms.fitness.data.DataSource;
import com.google.android.gms.fitness.data.DataType;
import com.google.android.gms.fitness.data.Field;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;

import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * Created by Administrator on 2018/1/25 0025.
 */

public class GoogleFitUtils {

    private String TAG = "GoogleFitUtils";

    /**
     * 添加数据到google fit
     */
    public Task<Void> insertData(Context context, Float data) {
        LogUtils.d(TAG, "=====添加数据到google fit:" + data);
        DataSet dataSet = insertFitnessData(context, data);
        return Fitness.getHistoryClient(context, GoogleSignIn.getLastSignedInAccount(context))
                .insertData(dataSet)
                .addOnCompleteListener(
                        new OnCompleteListener<Void>() {
                            @Override
                            public void onComplete(@NonNull Task<Void> task) {
                                if (task.isSuccessful()) {
                                    Log.i(TAG, "数据插入成功!");
                                } else {
                                    Log.e(TAG, "插入数据集时出现问题.", task.getException());
                                }
                            }
                        });
    }

    /**
     * 添加数据到google fit
     */
    private DataSet insertFitnessData(Context context, float data) {
        Log.i(TAG, "Creating a new data insert request.");
        // [START build_insert_data_request]
        // Set a start and end time for our data, using a start time of 1 hour before this moment.
        Calendar cal = Calendar.getInstance();
        Date now = new Date();
        cal.setTime(now);
        long endTime = cal.getTimeInMillis();
        cal.add(Calendar.HOUR_OF_DAY, -1);
        long startTime = cal.getTimeInMillis();

        // Create a data source
        DataSource dataSource =
                new DataSource.Builder()
                        .setAppPackageName(context)
                        .setDataType(DataType.TYPE_WEIGHT)
                        .setStreamName(TAG + " - weight")
                        .setType(DataSource.TYPE_RAW)
                        .build();
        // Create a data set
        DataSet dataSet = DataSet.create(dataSource);
        // For each data point, specify a start time, end time, and the data value -- in this case,
        // the number of new steps.
        DataPoint dataPoint =
                dataSet.createDataPoint().setTimeInterval(startTime, endTime, TimeUnit.MILLISECONDS);
        dataPoint.getValue(Field.FIELD_WEIGHT).setFloat(data);
        dataSet.add(dataPoint);
        // [END build_insert_data_req                 uest]
        return dataSet;
    }


    public boolean isOpenGoogleFit(Context context) {
        GoogleSignInAccount googleSignInAccount = GoogleSignIn.getLastSignedInAccount(context);
        boolean isGoogleFitOpen = (Boolean) SPUtil.get(context, "isGoogleFitOpen", false);
        boolean isSignIn = false;
        if (googleSignInAccount != null) {
            isSignIn = GoogleSignIn.hasPermissions(googleSignInAccount);
            LogUtils.d(TAG, "===========isSignIn:" + isSignIn);
        }
        if (isGoogleFitOpen && isSignIn) {
            return true;
        } else {
            return false;
        }
    }
}
